package com.ruoyi.common.utils;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

/**
 * <p>邮件发送工具类</p>
 * @author Bosen 2021/6/16 14:07
 */
@Component
public class MailUtil {

    @Value("${spring.mail.username}")
    String from;

    @Resource
    JavaMailSender sender;

    /*
     * 发送简单的文本邮件
     */
    public String sendSimpleMail(String to, String subject, String content) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(from);
        message.setTo(to);
        message.setSubject(subject);
        message.setText(content);
        try {
            sender.send(message);
            return "邮件发送成功";
        } catch (Exception e) {
            e.printStackTrace();
            return "邮件发送失败";
        }
    }

}